<?php

class ctrl_wishSellerAccount extends L {

    /**
     * 描述 ：Wish店铺Cookie页面展示
     * 作者 ：Zoro.Zhu
    **/
    public function index(){
        $this->view->_pagingHtml = $this->paging();
        $this->view->headTitle = 'Wish店铺Cookie';
        $this->display('/html/wishSellerAccount.html');
    }

    /**
     * 描述 : 获取分页
     * 作者 : Zoro.Zhu
     */
    public function &paging($params = array()){
        $sql = model_wishSellerAccount::searchSql($params);
        $config = array(
            '账号名称' => array(
                '_attr' => array(
                    'body' => '{`name`}',
                    'sort' => 'name'
                )
            ),
            '登录账号' => array(
                '_attr' => array(
                    'body' => '{`username`}',
                    'sort' => 'username'
                )
            ),
            '登录密码' => array(
                '_attr' => array(
                    'body' => '{`password`}',
                    'sort' => 'password'
                )
            ),
            'merchant_id' => '{`merchantId`}',
            'cookie' => '<div style="display: inline-block;overflow: hidden;width: 400px;word-break: keep-all;white-space: nowrap;text-overflow: ellipsis;line-height: 28px;float: left;">{`cookie`}</div>',
            'x-xsrftoken' => '{`xToken`}',
            '账号状态' => array(
                '_attr' => array(
                    'body' => '{`state`}',
                    'sort' => 'state'
                )
            ),
            '更新时间' => array(
                '_attr' => array(
                    'body' => '{`updDate`}',
                    'sort' => 'updDate'
                )
            ),
            '操作'=> "<button class=\"btn btn-oper btn-danger\" type=\"button\" onclick=\"wishSellerAccountObj.wishSellerAccountUpInfo('{`id`}');\">
                <span class=\"fa fa-plus\"></span>修改
            </button>",
            '_attr' => array(
                "attr" => array (
                    "table" => 'id=wishSellerAccountList class="table table-hover table-condensed table-striped table-scroll jsHoverDetails textC"',
                    "btr" => "align='center' class='of-paging_item_odd tr_{`id`}'",
                ),
                'data' => &$sql,
                'params' => &$params,
                'call' => array($this, 'asCall'),
                'method' => __METHOD__,
                'save' => 'auto',
                'empty' => '查无此数据'
            )
        );
        return $this->_com->paging($config);
    }

    /**
     * 描述 : 整理数据
     * 作者 : Zoro.Zhu
     */
    public function asCall(&$data){
        foreach($data['data'] as &$val){
            $val['state'] = $val['state'] == 1 ? '正常' : '失效';
        }
    }

    /**
     * 描述 : 新增和修改的页面展示
     * 作者 : Zoro.Zhu
     */
    public function wishSellerAccountUpInfo(){
        if(!empty($_REQUEST['id'])) {
            $wishSellerAccount = model_wishSellerAccount::getKeyInfo($_REQUEST['id']);
            if(!empty($wishSellerAccount)){ 
                $this->view->id = $wishSellerAccount[0]['id'];
                $this->view->name = $wishSellerAccount[0]['name'];
                $this->view->merchantId = $wishSellerAccount[0]['merchantId'];
                $this->view->cookie = $wishSellerAccount[0]['cookie'];
                $this->view->xToken = $wishSellerAccount[0]['xToken'];
                $this->view->state = $wishSellerAccount[0]['state'];
                $this->view->vsAccount = $wishSellerAccount[0]['vsAccount'];
                $this->view->updDate = $wishSellerAccount[0]['updDate'];
                $this->view->username = $wishSellerAccount[0]['username'];
                $this->view->password = $wishSellerAccount[0]['password'];
            }
        }
        $sql = "SELECT
             `account`
        FROM
            `vs_account`
        WHERE 1";
        $user = core_base::getUser('name');
        $sql .= " AND `addName` = '{$user}'";
        $sql .= " AND `type` = 'wish' ";
        $names = L::sql($sql);
        if(!empty($names)){
            $names = array_column($names, 'account');
        }
        $this->view->names = $names;
        $this->display('/html/wishSellerAccountUpInfo.html');
    }

    /**
     * 描述 ：新增或修改数据
     * 作者 ：Zoro.Zhu
    **/
    public function wishSellerAccountUpd(){
        $_POST['vsAccount'] = core_base::getUser('name');
        $_POST['state'] = 1;
        $_POST['updDate'] = date('Y-m-d H:i:s');
        //主键为空是新增，否则就是修改
        if(empty($_POST['id'])){
            $result = model_wishSellerAccount::insertSql($_POST);
            of_base_com_mq::set(['getProduct', $_POST['name']],['day' => 30, 'account' => $_POST['name'], 'getProductStats' => 1]);
        }else{
            $result = model_wishSellerAccount::updateSql($_POST);
        }
        if($result > 0){
            return array('state' => 200, 'info' => '操作成功');
        }else{
            return array('state' => 301, 'info' => '操作无效');
        }
    }

}
return true;